//
//  SearchForDiseaseAddPicsForIPhoneCell.m
//  RoadMaintain
//
//  Created by 岳学坤 on 2017/3/9.
//  Copyright © 2017年 岳学坤. All rights reserved.
//

#import "SearchForDiseaseAddPicsForIPhoneCell.h"

#define PhotoCellID @"cellid1"
@implementation SearchForDiseaseAddPicsForIPhoneCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUI];
    }
    return self;
}
- (NSMutableArray *)picsArray{
    if (_picsArray == nil) {
        _picsArray = [[NSMutableArray alloc]initWithCapacity:1];
    }
    return _picsArray;
}
- (NSMutableArray *)deleteArray{
    if (_deleteArray ==nil) {
        _deleteArray = [[NSMutableArray alloc]initWithCapacity:1];
    }
    return _deleteArray;
}
- (void)setUI{
    self.titleLab = [[UILabel alloc]init];
    self.titleLab.textColor = Black_Color;
    self.titleLab.font = [UIFont systemFontOfSize:14*ZitiRate];
    [self.contentView addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(45*WidthRate);
        make.left.mas_equalTo(10*WidthRate);
        make.right.mas_equalTo(-10*WidthRate);
    }];
    
   
    
    UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc]init];
    [fl setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    fl.minimumInteritemSpacing = 0;//垂直间距
    fl.minimumLineSpacing= 10*WidthRate;//水平间距
    fl.itemSize = CGSizeMake(70*WidthRate, 70*WidthRate);
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:fl];
    fl.sectionInset = UIEdgeInsetsMake(10*WidthRate, 10*WidthRate, 10*WidthRate, 10*WidthRate);
    self.collection.backgroundColor = Bai_Color;
    [self.collection registerClass:[PhotoColCell class] forCellWithReuseIdentifier:PhotoCellID];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    self.collection.showsVerticalScrollIndicator = NO;
    self.collection.showsHorizontalScrollIndicator = NO;
    self.collection.layer.borderColor = [Line_Color CGColor];
    self.collection.layer.borderWidth = 1;
    [self.contentView addSubview:self.collection];
    [self.collection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(45*WidthRate);
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
    }];
}
- (void)addValue:(NSArray *)arr{
    [self.picsArray removeAllObjects];
    [self.picsArray addObjectsFromArray:arr];
    [self.collection reloadData];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.isReset) {
        return self.picsArray.count+1;
    }else{
        return self.picsArray.count;
    }
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PhotoColCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:PhotoCellID forIndexPath:indexPath];
    cell.indexPath = indexPath;
    
    if (indexPath.row == self.picsArray.count) {
        cell.img.image = [UIImage imageNamed:@"add_picture"];
    
    }else{
        //    执行上传图片过程
       
        if ([self.picsArray[indexPath.row] isKindOfClass:[UIImage class]]) {
            cell.img.image = self.picsArray[indexPath.row];
        }else{
        [cell.img sd_setImageWithURL:[NSURL URLWithString:self.picsArray[indexPath.row]] placeholderImage:[UIImage imageNamed:@"1"]];
        }
      
    }
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.picsArray.count) {
        
            UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
            [actionSheet addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
                imagePicker.delegate = self;
                imagePicker.allowsEditing = NO;
                imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                imagePicker.videoQuality=UIImagePickerControllerQualityTypeLow;
                [self.con presentViewController:imagePicker animated:YES completion:nil];
            }]];
            [actionSheet addAction:[UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
                imagePicker.delegate = self;
                imagePicker.allowsEditing = NO;
                imagePicker.videoQuality=UIImagePickerControllerQualityTypeLow;
                imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                [self.con presentViewController:imagePicker animated:YES completion:nil];
            }]];
            [actionSheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
            [self.con presentViewController:actionSheet animated:YES completion:nil];
            

    }else{
        BigPicViewController *con = [[BigPicViewController alloc]init];
        con.delegate = self;
        con.allowDelete = self.isReset;
        con.imgArray = self.picsArray;
        [con.deleteArray removeAllObjects];
        [con.deleteArray addObjectsFromArray:self.deleteArray];
        con.imgIndex = indexPath.row;
        [self.con presentViewController:con animated:YES completion:nil];
    }
    
}

#pragma mark UIImagePickerController Delegate
- (void)bigPicDelegateWithImgArray:(NSMutableArray *)imgArray deleteArray:(NSMutableArray *)deleteArray{
    [self.picsArray removeAllObjects];
    [self.picsArray addObjectsFromArray:imgArray];
    [self.deleteArray removeAllObjects];
    [self.deleteArray addObjectsFromArray:deleteArray];
    [self.collection reloadData];
    if ([self.delegate respondsToSelector:@selector(picsForIphoneWithImgArray: AndDeleteArray:)]) {
        [self.delegate picsForIphoneWithImgArray:self.picsArray AndDeleteArray:self.deleteArray];
    }
}
//相机
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
    
    [self.picsArray addObject:image];
    if ([self.delegate respondsToSelector:@selector(picsForIphoneWithImgArray: AndDeleteArray:)]) {
        [self.delegate picsForIphoneWithImgArray:self.picsArray AndDeleteArray:self.deleteArray];
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
    [self.collection reloadData];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    return;
}



@end
